package com.ccrfid.rmc6.pojo.param;

import org.springframework.data.domain.Sort;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.QueryParam;
import java.util.ArrayList;
import java.util.List;

public class PageParam {
    @QueryParam("size")
    @DefaultValue("10")
    private Integer pageSize;

    @QueryParam("number")
    @DefaultValue("0")
    private Integer pageNumber;

    private List<Sort.Order> orders = new ArrayList<>();

    public PageParam(@QueryParam("sort") List<String> sortList) {
        for (String sortKV : sortList) {
            String[] kv = sortKV.split(",");
            if (kv.length == 2) {
                String property = kv[0];
                String direction = kv[1].toUpperCase();
                if (direction.equals("ASC")) {
                    orders.add(new Sort.Order(Sort.Direction.ASC, property));
                } else if (direction.equals("DESC")) {
                    orders.add(new Sort.Order(Sort.Direction.DESC, property));
                }
            }
        }
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(Integer pageNumber) {
        this.pageNumber = pageNumber;
    }

    public List<Sort.Order> getOrders() {
        return orders;
    }

    public void setOrders(List<Sort.Order> orders) {
        this.orders = orders;
    }

    @Override
    public String toString() {
        return "PageParam{" +
                "pageSize=" + pageSize +
                ", pageNumber=" + pageNumber +
                ", orders=" + orders +
                '}';
    }
}
